#Region " Imports "

Imports BlazeApps.Library.Polls
Imports BlazeApps.Library.Options

#End Region

Partial Class Usercontrols_Polls_PollViewer
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Protected Property PollGuid() As Guid
        Get
            Return GetGuidFromVS("PollID")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("PollID", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        SetPageTitle("View Poll")

        If (PollGuid = Guid.Empty) Then
            GetQueryStringValues()
        End If

        If Me.Page.IsPostBack = False Then
            SetViewed(PollGuid, False)

            If Not (PollGuid = Guid.Empty) Then
                LoadPoll()
                If IsPrintMeOrPopUpActive() = False Then
                    LoadOptions()
                End If
            End If
        End If

        SetPageState()

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveVote()
        Dim Item As PollResultsDetail = PollResultsDetail.NewPollResultsDetail()

        Item.PollId = PollGuid
        Item.PollChoice = ConvertToGuid(Me.uxPollAnswersRadioButtonList)
        Item.Userid = CurrentUserID

        Item.Save()

        Me.uxPollAnswersRadioButtonList.Visible = False

        LoadResults()
        Me.uxPollResultsRepeater.Visible = True
    End Sub

#End Region

#Region " Load Data "

    Private Sub LoadOptions()
        Dim Items As OptionDetail = OptionDetail.GetOptions(PollGuid)

        'Hide/Show Panels based on page settings
        Me.uxFavoritesPanel.Visible = (Items.AllowSubscriptions And EnableFavorites)
        Me.uxRaingPanel.Visible = (Items.AllowRanking And EnableRating)
        Me.uxCommentsPanel.Visible = (Items.AllowComments And EnableComments)
        Me.uxTagsPanel.Visible = (Items.AllowTags And EnableTags)
        Me.uxToolsPanel.Visible = (Items.AllowInfoTools And EnableTools)

        'Enable/Disable Panels based on page settings
        Me.uxFavoritesPanel.Enabled = (Items.AllowSubscriptions And EnableFavorites)
        Me.uxRaingPanel.Enabled = (Items.AllowRanking And EnableRating)
        Me.uxCommentsPanel.Enabled = (Items.AllowComments And EnableComments)
        Me.uxTagsPanel.Enabled = (Items.AllowTags And EnableTags)
        Me.uxToolsPanel.Enabled = (Items.AllowInfoTools And EnableTools)

    End Sub

    Private Sub LoadPoll()

        Dim Item As PollQuestionDetail = PollQuestionDetail.GetPollQuestionDetail(PollGuid)

        Me.uxPollQuestionLabel.Text = Item.PollQuestion
        Me.uxPollVotesLabel.Text = FormatTotalVotes(Item.AnswerCount)
        Me.uxCloseDateLabel.Text = FormatClosesOnDate(Item.ExpirationDate.ToShortDateString)

        If IsUserAuthenticated() Then
            Dim VoteCheck As PollVoteCheck = PollVoteCheck.GetPollVoteCheck(PollGuid, CurrentUserID)

            If VoteCheck.HasVoted Then
                LoadResults()
            Else
                If Item.ExpirationDate() <= Date.Now.Date() Then
                    LoadResults()
                Else
                    LoadAnswers()
                End If
            End If
        Else
            LoadResults()
        End If

        Me.uxFavorites.FavoriteType = BlazeApps.Library.Base.ItemType.Poll
        Me.uxPageTags.TagType = BlazeApps.Library.Base.ItemType.Poll

    End Sub

    Private Sub LoadAnswers()
        Dim List As PollAnswerParent = PollAnswerParent.GetPollAnswerParent(PollGuid)

        With Me.uxPollAnswersRadioButtonList
            .DataSource = List
            .DataTextField = "AnswerText"
            .DataValueField = "AnswerID"
            .DataBind()
        End With

        Me.uxPollResultsRepeater.Visible = False

    End Sub

    Private Sub LoadResults()
        Dim List As PollList = New PollList()

        With Me.uxPollResultsRepeater
            .DataSource = List.GetPollResults(PollGuid)
            .DataBind()
        End With

        Me.uxVoteButton.Visible = False

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxVoteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxVoteButton.Click
        SaveVote()
    End Sub

    Protected Sub uxPollResultsRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles uxPollResultsRepeater.ItemDataBound
        Dim TB As HtmlTable = CType(e.Item.FindControl("ResultsTable"), HtmlTable)

        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then

            If Convert.ToInt32(e.Item.DataItem("votepercentage")) > 0 Then
                TB.Style.Add("width", String.Format("{0}%", e.Item.DataItem("votepercentage")))
            End If

        End If

    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        PollGuid = GetGuidFromQueryString("id")
    End Sub

    Private Sub SetPageState()

        If IsPrintMeOrPopUpActive() = True Then
            Me.uxCommentsList.Visible = False
            Me.uxCommentsList.HideData = True
            Me.uxTools.Visible = False
            Me.uxTools.HideData = True
            Me.uxRating.Visible = False
            Me.uxRating.HideData = True
            Me.uxFavorites.Visible = False
            Me.uxFavorites.HideData = True
            Me.uxPageTags.Visible = False
            Me.uxPageTags.HideData = True
        End If

    End Sub

#End Region

End Class
